package com.streamhub.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

public class GWTStockTableDemo implements EntryPoint {
	private final VerticalPanel mainPanel = new VerticalPanel();
	private final String[] stocks = new String[] { "BA", "BAC", "C", "KO", "MCD", "WMT" };
	private FlexTable flexTable;
	private StreamHubGWTAdapter streamhub;

	public void onModuleLoad() {
		flexTable = createFlexTable();
		mainPanel.add(flexTable);
		mainPanel.addStyleName("mainPanel");
		RootPanel.get("stockTable").add(mainPanel);
		streamhub = new StreamHubGWTAdapter();
		streamhub.connect("${server.url}");
		addStocks();
	}

	private FlexTable createFlexTable() {
		FlexTable stockTable = new FlexTable();
		stockTable.setText(0, 0, "Symbol");
		stockTable.setText(0, 1, "Company");
		stockTable.setText(0, 2, "Last Price");
		stockTable.setText(0, 3, "Change");
		stockTable.addStyleName("stockTable");
		stockTable.getRowFormatter().addStyleName(0, "stockTableHeader");
		stockTable.getCellFormatter().addStyleName(0, 2, "stockTableNumeric");
		stockTable.getCellFormatter().addStyleName(0, 3, "stockTableNumeric");
		stockTable.setCellPadding(6);
		return stockTable;
	}

	private void addStocks() {
		for (String symbol : stocks) {
			Stock stock = new Stock(symbol);
			stock.addToFlexTable(flexTable);
			streamhub.subscribe(symbol, stock);
		}
	}
}
